package Logic

import (
	"fmt"
	"yz-go/Common"
	"yz-go/Models"

	"gorm.io/gorm"
)

type RequestData struct {
	LicenseCode string `json:"licenseCode"`
	IsAccurate  bool   `json:"isAccurate"`
	Sort        string `json:"sort"`
	UserName    string `json:"userName"`
}

type FieldOrder struct {
	Id         int64  `gorm:"id"`
	Mobile     string `gorm:"mobile"`
	SZCourseId string `gorm:"sz_course_id"`
}

func BindCourseValidate() (rd Common.ResData) {

	of := &[]FieldOrder{}

	field := "id,mobile,sz_course_id"
	url := "http://api.shark.shenzaokeji.com/third-party/third-party/pagePlayerInfoList"

	header := map[string]string{"Content-Type": "application/json"}

	requestData := RequestData{
		LicenseCode: "LC_baeVZwUuwqa6KxN270Gbh0djsfH2w",
		IsAccurate:  true,
		Sort:        "auth_time",
		UserName:    "",
	}

	Common.Err = Models.DB.Table("gz_order").Where("sz_course_bind = ? and sz_course_id  <> ?", 0, "0").Select(field).FindInBatches(of, 50,
		func(tx *gorm.DB, batch int) error {
			for _, v := range *of {
				fmt.Println(v)
				r := Common.SendPostRequest(url, header, requestData)
				fmt.Println(r)
				// if r["contents"].success && r["contents"].data.total > 0 {
				// 	if r["contents"].data.item[0].courseInfos {
				// 		fmt.Println("更新数据")
				// 		// 根据条件更新
				// 		up := map[string]interface{}{"sz_course_bind": 1, "sz_course_create_date": r["contents"].data.item[0].authTime, "sz_course_day": v.expireDay}
				// 		Models.DB.Table("gz_order").Where("active = ?", true).Updates(up)
				// 	}
				// }
			}
			return nil
		}).Error

	return Common.ResData{Code: Common.SUCCESS, Msg: "", Data: of}
}
